# -*- coding: utf-8 -*-

from osv import fields, osv

class informe(osv.osv):
    """
    informe
    """

    def _get_uid(self,cr,uid,ids):
        return uid

    def get_user_type(self,cr,uid,ids):
        res = None
        users = self.pool.get('res.users')
        curr_user = users.browse(cr, uid, uid)
        for group in curr_user.groups_id:
            if group.name == "Responsable":
                res = "Responsable"
            if group.name == "Usuario":
                res = "Usuario"
            if group.name == "Gestor":
                res = "Gestor"
            if group.name == "Administrador":
                res = "Administrador"
        return res

    def _get_id_rep_group_list(self,cr,uid,ids,context=None):
        arr = []
        curr_group_name = self.get_user_type(cr, uid, ids)
        reports_ids = self.pool.get('ir.actions.report.xml').search(cr,uid,[])
        reports_all = self.pool.get('ir.actions.report.xml').browse(cr,uid,reports_ids)
        for rep in reports_all:
            for grp in rep.groups_id:
                if grp.name == curr_group_name:
                    arr.append(rep.id)
        return arr
    
    def get_id_rep_group_dict(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        res[ids[0]] = self._get_id_rep_group_list(cr, uid, ids, context)
        return res
        
    '''
    Campos de la clase
    '''
    _name = 'informe'
    _columns = {
            'report_id': fields.many2one('ir.actions.report.xml', 'Informe', select=True, required=True),
            'id_rep_group':fields.function(get_id_rep_group_dict, method=True, type='many2many', store=False, fnct_search=None),
            'user_uid': fields.many2one('res.users', 'Creado por'),  # Registro
    }
    
    
    _defaults = {
            'id_rep_group': _get_id_rep_group_list,
            'user_uid': _get_uid,
    }
    
    def print_report(self, cr, uid, ids, context={}):
        curr_rep = self.browse(cr, uid, ids[0], context).report_id
        report_id = self.pool.get('ir.actions.report.xml').search(cr, uid, [('report_name', '=', curr_rep.report_name)])[0]
        report_xml = self.pool.get('ir.actions.report.xml').browse(cr, uid, report_id)
        users = self.pool.get('res.users')
        curr_user = users.browse(cr, uid, uid)
        args = []
        if curr_user.organization_id.id:
            args = [('organization_id.id','=',curr_user.organization_id.id)]
        print_ids = self.pool.get(curr_rep.model).search(cr, uid,args)
        data = {'model':report_xml.model, 'ids':print_ids, 'id':report_id, 'report_type': 'aeroo'}
        return {
            'type': 'ir.actions.report.xml',
            'report_name': report_xml.report_name,
            'datas': data,
            'context':context
        }
        
    def create(self, cr, uid, vals, context=None):
        #EVITA DUPLICADOS
        reports_ids = self.pool.get('informe').search(cr,uid,[])
        reports_all = self.pool.get('informe').browse(cr,uid,reports_ids)
        for rep in reports_all:
            print rep.report_id.id
            print vals['report_id']
            if rep.report_id.id == vals['report_id']:
                raise osv.except_osv('No permitido', u'El informe seleccionado ya existe!')
        return super(informe,self).create(cr, uid, vals, context)


informe()
